import { Controller, Get, Post, Body, HttpCode, Query, UseGuards, Headers } from '@nestjs/common';
import { FeedbackService } from './feedback.service';
import { ApiResponse, ApiOperation, ApiBody, ApiQuery, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { CreateFeedbackDto, GetFeedbackPageDot } from './dto/feedback.dto';
import { AuthGuard } from '@nestjs/passport';
import { FeedbackResult } from 'src/modules/admin/feedback/entities/feedback.entity';
import { JwtService } from '@nestjs/jwt';
import { TokenDto } from 'src/common/auth/constants';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';

@ApiBearerAuth() // swagger文档设置token
@ApiTags('问题反馈')
@Controller('home/feedback')
export class FeedbackController {
  constructor(
    private readonly feedbackService: FeedbackService,
    private readonly jwtService: JwtService,
  ) { }
  /** 问题反馈列表 */
  // @HttpCode(200)
  // @Get('/getFeedbackPage')
  // @ApiResponse({
  //   status: 200,
  //   description: '问题反馈列表',
  //   type: FeedbackResult,
  // })
  // @ApiOperation({
  //   tags: ['获取问题反馈列表'],
  //   summary: '获取问题反馈列表',
  //   operationId: 'getFeedbackPage',
  // })
  // @UseGuards(AuthGuard('userJwt')) // 自定义守卫
  // getFeedbackPage(@Query() params: GetFeedbackPageDot, @Headers('authorization') authorization: string) {
  //   if (!isTrue(authorization)) {
  //     throwHttp('获取token失败');
  //     return
  //   }
  //   const token = authorization.split(' ');
  //   const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
  //   return this.feedbackService.getFeedbackPage(params, user.userId);
  // }
  @HttpCode(200)
  @Post('/createFeedback')
  @ApiQuery({
    description: '创建问题反馈',
    type: CreateFeedbackDto,
  })
  @ApiBody({ description: '创建问题反馈', type: CreateFeedbackDto })
  @ApiOperation({ summary: '创建问题反馈', operationId: 'createFeedback' })
  @ApiResponse({ status: 200, type: String })
  @UseGuards(AuthGuard('userJwt'))
  createFeedback(@Body() body: CreateFeedbackDto, @Headers('authorization') authorization: string) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.feedbackService.createFeedback(body, user.userId);
  }
}
